package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.Comparator;
import java.util.function.Consumer;

/**
 * @ClassName TableViewStudentProDemo
 * @Description 自定义排序
 * @Author wangss
 * @date 2020.06.13 20:35
 * @Version 1.0
 */
public class TableViewCustomSortDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        StudentPro s1 = new StudentPro("小a", 10, 50d, false);
        StudentPro s2 = new StudentPro("小b", 11, 65d, true);
        StudentPro s3 = new StudentPro("小c", 11, 74d, true);
        StudentPro s4 = new StudentPro("小d", 9, 98d, true);

        ObservableList<StudentPro> list = FXCollections.observableArrayList();
        list.addAll(s1, s2, s3, s4);

        TableView<StudentPro> tableView = new TableView<>(list);

        TableColumn<StudentPro, String> name = new TableColumn<>("姓名");
        name.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<StudentPro, Integer> age = new TableColumn<>("年龄");
        age.setCellValueFactory(new PropertyValueFactory<>("age"));

        TableColumn<StudentPro, Double> score = new TableColumn<>("成绩");
        score.setCellValueFactory(new PropertyValueFactory<>("score"));


        TableColumn<StudentPro, Boolean> flag = new TableColumn<>("是否及格");
        flag.setCellValueFactory(new PropertyValueFactory<>("flag"));

        tableView.getColumns().addAll(name, age, score, flag);

        Button button = new Button("button");

        AnchorPane.setTopAnchor(tableView, 50d);
        AnchorPane.setLeftAnchor(tableView, 50d);
        anchorPane.getChildren().addAll(button, tableView);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("TableView");
        primaryStage.setResizable(false);
        primaryStage.show();

        // 自定义排序
        tableView.setSortPolicy(new Callback<TableView<StudentPro>, Boolean>() {
            @Override
            public Boolean call(TableView<StudentPro> param) {
                param.getColumns().forEach(new Consumer<TableColumn<StudentPro, ?>>() {
                    @Override
                    public void accept(TableColumn<StudentPro, ?> t) {
                        if (t.getText().equals("姓名") && t.getSortType() == TableColumn.SortType.ASCENDING) {
                            // 自定义图标
                            t.setSortNode(new Label("升"));

                            param.getItems().sort(new Comparator<StudentPro>() {
                                @Override
                                public int compare(StudentPro o1, StudentPro o2) {
                                    return o1.getName().compareTo(o2.getName());
                                }
                            });
                        } else if (t.getText().equals("姓名") && t.getSortType() == TableColumn.SortType.DESCENDING) {
                            // 自定义图标
                            t.setSortNode(new Label("降"));

                            param.getItems().sort(new Comparator<StudentPro>() {
                                @Override
                                public int compare(StudentPro o1, StudentPro o2) {
                                    return o2.getName().compareTo(o1.getName());
                                }
                            });
                        }
                    }
                });
                // false禁止排序
                return true;
            }
        });

        // 设置某个是否可以排序
        name.setSortable(false);
        // 第二种自定义排序
        name.setComparator(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return 0;
            }
        });

        // 第三种自定义排序 对数据源进行排序
        tableView.getItems().sort(new Comparator<StudentPro>() {
            @Override
            public int compare(StudentPro o1, StudentPro o2) {
                return 0;
            }
        });

        // 列的展示方向
        tableView.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);

        tableView.setEditable(true);
        flag.setCellFactory(CheckBoxTableCell.forTableColumn(flag));
    }
}
